package com.ruoyi.st.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.response.JsonResult;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.st.domain.StUser;
import com.ruoyi.st.domain.param.UserLoginParam;
import com.ruoyi.st.domain.vo.UserVo;
import com.ruoyi.st.service.IStUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户Controller
 * 
 * @author ruoyi
 * @date 2024-11-07
 */
@Slf4j
@RestController
@RequestMapping("/st/user")
public class StUserController extends BaseController
{
    @Autowired
    private IStUserService stUserService;

    /**
     * 用户登录
     */
//    @PostMapping("/login")
//    public JsonResult login(@RequestBody StUser stUser){
//        log.debug("用户登录:userLoginParam={}",stUser);
//        List<StUser> list = stUserService.selectStUserList(stUser);
//        StUser stUser1 = list.get(0);
//        return new JsonResult(stUser1);
//    }
    /**
     * 用户登录
     */
    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginParam userLoginParam){
        log.debug("用户登录:userLoginParam={}",userLoginParam);
        UserVo userVo = stUserService.selectStUserByUserParam(userLoginParam);
        return JsonResult.ok(userVo);
    }
    /**
     * 查询用户列表
     */
    @PreAuthorize("@ss.hasPermi('st:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(StUser stUser)
    {
        startPage();
        List<StUser> list = stUserService.selectStUserList(stUser);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @PreAuthorize("@ss.hasPermi('st:user:export')")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StUser stUser)
    {
        List<StUser> list = stUserService.selectStUserList(stUser);
        ExcelUtil<StUser> util = new ExcelUtil<StUser>(StUser.class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('st:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(stUserService.selectStUserById(id));
    }

    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('st:user:add')")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StUser stUser)
    {
        return toAjax(stUserService.insertStUser(stUser));
    }

    /**
     * 修改用户
     */
    @PreAuthorize("@ss.hasPermi('st:user:edit')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StUser stUser)
    {
        return toAjax(stUserService.updateStUser(stUser));
    }

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('st:user:remove')")
    @Log(title = "用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(stUserService.deleteStUserByIds(ids));
    }
}
